package com.hamizhong.iflowers.web.action.admin.statics;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.ITargetService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.TargetVO;

public class AjaxQueryTargetAction extends MyBaseAjaxAction {

	private ITargetService iTargetService = null;
	
	public ITargetService getiTargetService() {
		return iTargetService;
	}
	public void setiTargetService(ITargetService iTargetService) {
		this.iTargetService = iTargetService;
	}

	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryTargetAction...");
		String pageNum = getRQ().getParameter("pageNum");
		IOPResultDto<IObjectVO> resultDto = iTargetService.findAll(Integer.valueOf(pageNum), MyConstants.PAGE_SIZE_COMMON_ADMIN);
		IObjectVO iObjectVO = resultDto.getFirstResult();
		List<TargetVO> targetVOList = iObjectVO.getTargetVOList();
		int totalTargetCnt = iObjectVO.getTotalTargetCnt();
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		//设置总条数
		jo.put("totalCnt", totalTargetCnt+"");
		//设置当前页
		jo.put("pageNum", pageNum);
		//设置总页数
		jo.put("totalPageCnt", MyTools.convertItemCnt2PageCnt(totalTargetCnt, MyConstants.PAGE_SIZE_COMMON_ADMIN)+"");
		for (TargetVO targetVO : targetVOList) {
			jobj = new JSONObject();
			jobj.put("targetId", targetVO.getTargetId());
			jobj.put("targetName", targetVO.getTargetName());
			jobj.put("sortId", targetVO.getSortId());
			jobj.put("remarks", targetVO.getRemarks());
			ja.add(jobj);
		}
		jo.put("data", ja.toString());
		jo.put("result", MyConstants.AJAX_SUCCESS);
		return jo;
	}

}
